package com.apple1.blogservices.impl;

import com.apple1.blogdao.ArticleMapper;
import com.apple1.blogdao.CategoryMapper;
import com.apple1.blogentity.Article;
import com.apple1.blogentity.ArticleExample;
import com.apple1.blogentity.Category;
import com.apple1.blogentity.CategoryExample;
import com.apple1.blogentity.vo.CategoryVo;
import com.apple1.blogservices.CategoryService;
import com.apple1.commonutil.BeanMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  分类服务实现类
 * @author chencheng
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    ArticleMapper articleMapper;


    /**
     * 方法名: 添加分类
     * 处理逻辑:
     * note(注意):
     * @param category
     * @return int
     */
    @Override
    public int addCategory(CategoryVo category) {
        if (category == null){
            return 0;
        }
        Category mapBean = BeanMapUtil.getMapBean(category, Category.class);
        mapBean.setTableId(null);
        if (mapBean.getCategory() == null){
            return 0;
        }
        return categoryMapper.insert(mapBean);
    }

    /**
     * 方法名: 删除分类
     * 处理逻辑:
     * note(注意): 事务
     * @param category_id
     * @return int
     */
    @Override
    public int delCategoryById(Integer category_id) {
        if (category_id == null){
            return 0;
        }
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andTableIdEqualTo(category_id);

        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria1 = articleExample.createCriteria();
        criteria1.andCategoryEqualTo(category_id);
        Article article = new Article();
        article.setCategory(0);

        articleMapper.updateByExampleSelective(article,articleExample);

        return categoryMapper.deleteByExample(categoryExample);
    }
    /**
     * 方法名: 更新分类
     * 处理逻辑:
     * note(注意):
     * @param category
     * @return int
     */
    @Override
    public int updateCategory(CategoryVo category) {
        if (category == null || category.getTableId() == null || category.getCategory() == null){
            return 0;
        }
        Category mapBean = BeanMapUtil.getMapBean(category, Category.class);
        mapBean.setTableId(null);
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andTableIdEqualTo(category.getTableId());

        return categoryMapper.updateByExampleSelective(mapBean,categoryExample);
    }
    /**
     * 方法名: 根据id查询分类信息
     * 处理逻辑:
     * note(注意):
     * @param category_id
     * @return com.apple1.blogentity.vo.CategoryVo
     */
    @Override
    public CategoryVo selectCategoryById(Integer category_id) {
        if (category_id == null){
            return null;
        }
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andTableIdEqualTo(category_id);

        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        if (categories == null || categories.size() == 0){
            return null;
        }
        Category category = categories.get(0);

        return BeanMapUtil.getMapBean(category,CategoryVo.class);
    }
}
